home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / JumpingBox / MouseTrack.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  2.8 KB  |  87 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Event;
  4. import java.awt.Graphics;
  5.  
  6. public class MouseTrack extends Applet {
  7.    // $FF: renamed from: mx int
  8.    int field_0;
  9.    // $FF: renamed from: my int
  10.    int field_1;
  11.    int onaroll;
  12.  
  13.    public void init() {
  14.       this.onaroll = 0;
  15.       ((Component)this).resize(500, 500);
  16.    }
  17.  
  18.    public void paint(Graphics g) {
  19.       g.drawRect(0, 0, ((Component)this).size().width - 1, ((Component)this).size().height - 1);
  20.       this.field_0 = (int)(Math.random() * (double)1000.0F) % (((Component)this).size().width - ((Component)this).size().width / 10);
  21.       this.field_1 = (int)(Math.random() * (double)1000.0F) % (((Component)this).size().height - ((Component)this).size().height / 10);
  22.       g.drawRect(this.field_0, this.field_1, ((Component)this).size().width / 10 - 1, ((Component)this).size().height / 10 - 1);
  23.    }
  24.  
  25.    public boolean mouseDown(Event evt, int x, int y) {
  26.       ((Component)this).requestFocus();
  27.       if (this.field_0 < x && x < this.field_0 + ((Component)this).size().width / 10 - 1 && this.field_1 < y && y < this.field_1 + ((Component)this).size().height / 10 - 1) {
  28.          if (this.onaroll > 0) {
  29.             switch (this.onaroll % 4) {
  30.                case 0:
  31.                   ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/tiptoe.thru.the.tulips.au");
  32.                   break;
  33.                case 1:
  34.                   ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/danger,danger...!.au");
  35.                   break;
  36.                case 2:
  37.                   ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/adapt-or-die.au");
  38.                   break;
  39.                case 3:
  40.                   ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/cannot.be.completed.au");
  41.             }
  42.  
  43.             ++this.onaroll;
  44.             if (this.onaroll > 5) {
  45.                ((Applet)this).getAppletContext().showStatus("You're on your way to THE HALL OF FAME:" + this.onaroll + "Hits!");
  46.             } else {
  47.                ((Applet)this).getAppletContext().showStatus("YOU'RE ON A ROLL:" + this.onaroll + "Hits!");
  48.             }
  49.          } else {
  50.             ((Applet)this).getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  51.             ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/that.hurts.au");
  52.             this.onaroll = 1;
  53.          }
  54.       } else {
  55.          ((Applet)this).getAppletContext().showStatus("You hit nothing at (" + x + ", " + y + "), exactly\n");
  56.          ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/thin.bell.au");
  57.          this.onaroll = 0;
  58.       }
  59.  
  60.       ((Component)this).repaint();
  61.       return true;
  62.    }
  63.  
  64.    public boolean mouseMove(Event evt, int x, int y) {
  65.       if (x % 3 == 0 && y % 3 == 0) {
  66.          ((Component)this).repaint();
  67.       }
  68.  
  69.       return true;
  70.    }
  71.  
  72.    public void mouseEnter() {
  73.       ((Component)this).repaint();
  74.    }
  75.  
  76.    public void mouseExit() {
  77.       this.onaroll = 0;
  78.       ((Component)this).repaint();
  79.    }
  80.  
  81.    public void keyDown(int key) {
  82.       ((Component)this).requestFocus();
  83.       this.onaroll = 0;
  84.       ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/ip.au");
  85.    }
  86. }
  87.